/******************************************************************************
 *
 * @file       main.cpp
 * @brief      Qt中Concurrent模块
 *
 * @author     xiYan
 * @date       2025/06/08
 * @history
 *****************************************************************************/
#include <QCoreApplication>
#include <QtConcurrent>
#include <QThread>
#include <QThreadPool>
#include <QDebug>
#include <QString>
#include <QFutureWatcher>
#include "test.h"

// 示例1
/*
void loop()
{
    for(int i = 0;i < 10;i++)
    {
        qInfo() << "Loop" << i << "on: " << QThread::currentThread();
    }
}

void test()
{
    qInfo() << "Test on :" << QThread::currentThread();
    loop();
}

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    QThread::currentThread()->setObjectName("Main");

    QThreadPool pool;
    QFuture<void> future = QtConcurrent::run(&pool,test);
    qInfo() << "Main on :" << QThread::currentThread();

    return a.exec();
}
*/
// 示例2
/*
void test(QString name,int max)
{
    for(int i = 0;i < 10;i++)
        qInfo() << name << i << QThread::currentThread();
}

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    QThread::currentThread()->setObjectName("Main");

    QString name = "name";
    QFuture<void> future = QtConcurrent::run(test,name,5);

    future.waitForFinished();   // block
    qInfo() << "Done";

    return a.exec();
}
*/

void multiply(int &value)
{
    qInfo() << "Called" << value;
    value = value * 10;
}

int main(int argc, char *argv[])
{

    QCoreApplication a(argc, argv);
    QThread::currentThread()->setObjectName("Main");

    QList<int> list;
    for(int i = 0;i < 100;i++)
    {
        list.append(i);
    }

    QFutureWatcher<void> watcher;
    Test test;
    QObject::connect(&watcher,&QFutureWatcher<void>::finished,&test,&Test::finished);
    QFuture<void> future = QtConcurrent::map(list,&multiply);
    watcher.setFuture(future);

    qInfo() << "Back in main";
    watcher.waitForFinished(); //Blocking
    qInfo() << "Done";
    qInfo() << list;

    return a.exec();
}













